﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PylonLiveView
{
    class DuanLuJianCe_test
    {
        public bool JianCe(HObject h)
        {

            bool value;
            // Local iconic variables 

            HObject ho_Image, ho_GrayImage1, ho_ROI1, ho_ImageIC1;
            HObject ho_ImagePartIC1, ho_ImageScaled, ho_Regions, ho_RegionDilation;
            HObject ho_RegionOpening, ho_RegionFillUp, ho_RegionDifference;
            HObject ho_ConnectedRegions, ho_SelectedRegions;

            // Local control variables 

            HTuple hv_Width = null;
            HTuple hv_Height = null, hv_Area1 = null, hv_Row1 = null;
            HTuple hv_Column1 = null, hv_Area2 = null, hv_Row2 = null;
            HTuple hv_Column2 = null, hv_Area = null, hv_Row = null;
            HTuple hv_Column = null;
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_GrayImage1);
            HOperatorSet.GenEmptyObj(out ho_ROI1);
            HOperatorSet.GenEmptyObj(out ho_ImageIC1);
            HOperatorSet.GenEmptyObj(out ho_ImagePartIC1);
            HOperatorSet.GenEmptyObj(out ho_ImageScaled);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_RegionDilation);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
            HOperatorSet.GenEmptyObj(out ho_RegionDifference);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            try
            {
                //图片路径自己加
                //list_image_files ('C:/Users/于立昆/Desktop/image', 'default', [], ImageFiles)
                //HOperatorSet.OpenFramegrabber("GigEVision2", 0, 0, 0, 0, 0, 0, "progressive", -1, "default", -1, "false", "default", "003053252b41_Basler_acA244020gm", 0, -1, out hv_AcqHandle);
                //HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
                //ho_Image.Dispose();
                //HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
                ho_Image = h;
                //close_framegrabber (AcqHandle)
                //dev_update_window ('off')
                if (HDevWindowStack.IsOpen())
                {
                    //dev_close_window ()
                }
                //read_image (Image, ImageFiles[Index])
                HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                //dev_open_window (0, 0, Width/4, Height/4, 'black', WindowHandle)
                //dev_set_window (WindowHandle)
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
                }
                ho_GrayImage1.Dispose();
                HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage1);
                ho_ROI1.Dispose();

                HOperatorSet.GenCircle(out ho_ROI1, 925.5, 1277.5, 1069.49);

                ho_ImageIC1.Dispose();
                HOperatorSet.ReduceDomain(ho_GrayImage1, ho_ROI1, out ho_ImageIC1);
                ho_ImagePartIC1.Dispose();
                HOperatorSet.CropDomain(ho_ImageIC1, out ho_ImagePartIC1);

                ho_ImageScaled.Dispose();
                HOperatorSet.ScaleImage(ho_ImagePartIC1, out ho_ImageScaled, 3.49315, -636);
                ho_Regions.Dispose();
                HOperatorSet.Threshold(ho_ImageScaled, out ho_Regions, 0, 221);
                HOperatorSet.AreaCenter(ho_Regions, out hv_Area1, out hv_Row1, out hv_Column1);

                ho_RegionDilation.Dispose();
                HOperatorSet.DilationRectangle1(ho_Regions, out ho_RegionDilation, 3, 3);
                ho_RegionOpening.Dispose();
                HOperatorSet.OpeningRectangle1(ho_RegionDilation, out ho_RegionOpening, 2,
                    2);
                HOperatorSet.AreaCenter(ho_RegionOpening, out hv_Area2, out hv_Row2, out hv_Column2);

                ho_RegionFillUp.Dispose();
                HOperatorSet.FillUp(ho_RegionOpening, out ho_RegionFillUp);

                ho_RegionDifference.Dispose();
                HOperatorSet.Difference(ho_RegionFillUp, ho_RegionOpening, out ho_RegionDifference
                    );
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_RegionDifference, out ho_ConnectedRegions);
                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions, "max_area",
                    70);
                HOperatorSet.AreaCenter(ho_SelectedRegions, out hv_Area, out hv_Row, out hv_Column);
                if ((int)(new HTuple(hv_Area.TupleGreater(10))) != 0)
                {
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_ImagePartIC1, HDevWindowStack.GetActive());
                    }
                    //set_display_font (WindowHandle, 30, 'mono', 'true', 'false')
                    //disp_message (WindowHandle, '不合格', 'window', 0, 0, 'red', 'true')
                    value = false;
                }
                else
                {
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_ImagePartIC1, HDevWindowStack.GetActive());
                    }
                    //set_display_font (WindowHandle, 30, 'mono', 'true', 'false')
                    //disp_message (WindowHandle, '合格', 'window', 0, 0, 'green', 'true')
                    value = true;

                }
                //set_display_font (WindowHandle, 18, 'mono', 'true', 'false')
                //disp_continue_message (WindowHandle, 'black', 'true')
                // stop(...); only in hdevelop


            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_Image.Dispose();
                ho_GrayImage1.Dispose();
                ho_ROI1.Dispose();
                ho_ImageIC1.Dispose();
                ho_ImagePartIC1.Dispose();
                ho_ImageScaled.Dispose();
                ho_Regions.Dispose();
                ho_RegionDilation.Dispose();
                ho_RegionOpening.Dispose();
                ho_RegionFillUp.Dispose();
                ho_RegionDifference.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions.Dispose();

                throw HDevExpDefaultException;
            }
            ho_Image.Dispose();
            ho_GrayImage1.Dispose();
            ho_ROI1.Dispose();
            ho_ImageIC1.Dispose();
            ho_ImagePartIC1.Dispose();
            ho_ImageScaled.Dispose();
            ho_Regions.Dispose();
            ho_RegionDilation.Dispose();
            ho_RegionOpening.Dispose();
            ho_RegionFillUp.Dispose();
            ho_RegionDifference.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_SelectedRegions.Dispose();

            return value;
        }
    }
}
